<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2021/7/25
  Time: 18:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="static/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="static/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="static/css/animate.min.css" rel="stylesheet">
    <link href="static/css/myAlert.css" rel="stylesheet">
    <link href="static/css/style.min.css?v=4.0.0" rel="stylesheet"><base target="_blank">
    <style type="text/css">
        button{
            float: right;
        }
        .ibox-content{
            width: 700px;
            box-shadow: 2px 2px 5px #bababa;
        }
    </style>
</head>
<body>
<div style="width: 60%;margin-left: 20%;margin-top: 10%" class="allRole">
    <h2 style="text-align: center ;color: #07c227">角色列表</h2>
    <table class="table table-hover" >
        <thead>
        <tr >
            <th>id</th>
            <th>名称</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody class="first">

        </tbody>
    </table>
    第<span id="spanPageNum"></span>页/共<span id="spanTotalPage"></span>页
    <button type="button" class="btn btn-outline btn-primary increase">下一页</button>
    <button type="button" class="btn btn-outline btn-primary reduce">上一页</button>
</div>
<%--划分权限--%>
<div class="ibox-content addMenu" style="margin-left: 20%;margin-top: 10%">
    <form class="fff" action="addMenuToRole">
        <h3>分配权限</h3>
        <input type="hidden"  name="rid" class="addMenu-rid">
        <table class="table table-bordered">
            <thead>
            <tr>
                <th></th>
                <th>id</th>
                <th>权限</th>
            </tr>
            </thead>
            <tbody class="Menuck">
            </tbody>
        </table>
        <input type="submit" class="btn btn-w-m btn-success noMenu GO"value="确认添加"/>
        <input type="button" class="btn btn-w-m btn-default noMenu" value="取消添加"/>
    </form>
</div>
<script src="static/js/jquery.min.js?v=2.1.4"></script>
<script src="static/js/bootstrap.min.js?v=3.3.5"></script>
<script src="static/js/plugins/iCheck/icheck.min.js"></script>
<script src="static/js/myAlert.js"></script>
<script src="static/js/jquery.form.min.js"></script>
<script>
    $(function (){
        $("#spanPageNum").html(1);
        $(".addMenu").hide();
        $.ajax({
            url:"findAllRole",
            method:"post",
            data:{page:1},
            dataType:"json",
            success:function (data){
                //分页查询role列表
                for (let i = 0; i < data.length; i++) {
                    $(".first").append("   <tr>\n" +
                        "            <td id='rid'>"+data[i].rid+"</td>" +
                        "            <td>"+data[i].rname+"</td>" +
                        "            <td><input type='button' class='btn btn-outline btn-warning' value='分配权限'/>" +
                        "                <input type='button' class='btn btn-outline btn-danger GO'value='删除'/></td>" +
                        "        </tr>")
                }
                //分配权限
                $(".btn-warning").click(function (){
                    let rid= $(this).parents("tr").find("#rid").text()
                    $(".allRole").hide();
                    $(".addMenu").show();
                    $.ajax({
                        url:"findMneuNoRid",
                        type:"post",
                        data:{rid:rid},
                        dataType:"json",
                        success:function (data){
                            $(".Menuck").html('');
                            for (var i=0;i<data.length;i++){
                                $(".Menuck").append("   <tr> <td><input type='checkbox' name='Menus' value='"+data[i].mid+"'></td> <td id='rid'>"+data[i].mid+"</td><td>"+data[i].mname+"</td>" +
                                    "<td>"+data[i].murl+"</td></tr>")
                            }
                        }
                    })
                    $(".addMenu-rid").val(rid)
                })
                //取消分配权限
                $(".noMenu").click(function (){
                    $(".allRole").show();
                    $(".addMenu").hide();
                })
                var malert= {
                    title: "Tip",
                    msg: "操作成功",
                }
                $(".GO").click(function (){
                    MyAlert(malert);
                })
                //阻止form表单跳转
                $(".fff").submit(function (){
                    $(this).ajaxSubmit()
                    return false;
                })
                //删除角色
                $(".btn-danger").click(function (){
                    let rid= $(this).parents("tr").find("#rid").text()
                    $.ajax({
                        url:"deleteRole",
                        method:"post",
                        data:{rid:rid},
                        success:function (){
                            alert("删除成功，请刷新页面")
                        },
                        error:function (){
                            window.location.herf="404.jsp"
                        }
                    })
                })
            },
            error:function (){
                window.location.herf="404.jsp"
            }
        })
        //查询总页数
        $.ajax({
            url:"getRolePages",
            method:"post",
            success:function (data){
                pageSize=data;
                $("#spanTotalPage").html(pageSize);
            },
            error:function (){
                window.location.herf="404.jsp"
            }
        })
        //下一页
        $(".increase").click(function (){
            let oldPage=parseInt($("#spanPageNum").text());
            if (oldPage<pageSize){
                let newPage=oldPage+1;
                $("#spanPageNum").html(newPage);
                $.ajax({
                    url:"findAllRole",
                    method:"post",
                    data:{page:newPage},
                    dataType:"json",
                    success:function (data) {
                        $(".first").html("");
                        for(let u=0;u<data.length;u++){
                            $(".first").append("   <tr>\n" +
                                "            <td id='rid'>"+data[u].rid+"</td>" +
                                "            <td>"+data[u].rname+"</td>" +
                                "            <td><input type='button' class='btn btn-outline btn-warning' value='分配权限'/>" +
                                "                <input type='button' class='btn btn-outline btn-danger GO'value='删除'/></td>" +
                                "        </tr>")
                        }
                        var malert= {
                            title: "Tip",
                            msg: "操作成功",
                        }
                        $(".GO").click(function (){
                            MyAlert(malert);
                        })
                        //阻止form表单跳转
                        $(".fff").submit(function (){
                            $(this).ajaxSubmit()
                            return false;
                        })
                        //分配权限
                        $(".btn-warning").click(function (){
                            let rid= $(this).parents("tr").find("#rid").text()
                            $(".allRole").hide();
                            $(".addMenu").show();
                            $.ajax({
                                url:"findMneuNoRid",
                                type:"post",
                                data:{rid:rid},
                                dataType:"json",
                                success:function (data){
                                    $(".Menuck").html('');
                                    for (var i=0;i<data.length;i++){
                                        $(".Menuck").append("   <tr> <td><input type='checkbox' name='Menus' value='"+data[i].mid+"'></td> <td id='rid'>"+data[i].mid+"</td><td>"+data[i].mname+"</td>" +
                                            "<td>"+data[i].murl+"</td></tr>")
                                    }
                                }
                            })
                            $(".addMenu-rid").val(rid)
                        })
                        //取消分配权限
                        $(".noMenu").click(function (){
                            $(".allRole").show();
                            $(".addMenu").hide();
                        })
                        //删除角色
                        $(".btn-danger").click(function (){
                            let rid= $(this).parents("tr").find("#rid").text()
                            $.ajax({
                                url:"deleteRole",
                                method:"post",
                                data:{rid:rid},
                                success:function (){
                                    alert("删除成功，请刷新页面")
                                },
                                error:function (){
                                    window.location.herf="404.jsp"
                                }
                            })
                        })
                    }
                })
                }
        })
        //上一页
        $(".reduce").click(function (){
            let oldPage=parseInt($("#spanPageNum").text());
            if (oldPage>1){
                let newPage=oldPage-1;
                $("#spanPageNum").html(newPage);
                $.ajax({
                    url:"findAllRole",
                    method:"post",
                    data:{page:newPage},
                    dataType:"json",
                    success:function (data){
                        $(".first").html("")
                        for(let u=0;u<data.length;u++){
                            $(".first").append("   <tr>\n" +
                                "            <td id='rid'>"+data[u].rid+"</td>" +
                                "            <td>"+data[u].rname+"</td>" +
                                "            <td><input type='button' class='btn btn-outline btn-warning' value='分配权限'/>" +
                                "                <input type='button' class='btn btn-outline btn-danger GO'value='删除'/></td>" +
                                "        </tr>")
                        }
                        //分配权限
                        $(".btn-warning").click(function (){
                            let rid= $(this).parents("tr").find("#rid").text()
                            $(".allRole").hide();
                            $(".addMenu").show();
                            $.ajax({
                                url:"findMneuNoRid",
                                type:"post",
                                data:{rid:rid},
                                dataType:"json",
                                success:function (data){
                                    $(".Menuck").html('');
                                    for (var i=0;i<data.length;i++){
                                        $(".Menuck").append("   <tr> <td><input type='checkbox' name='Menus' value='"+data[i].mid+"'></td> <td id='rid'>"+data[i].mid+"</td><td>"+data[i].mname+"</td>" +
                                            "<td>"+data[i].murl+"</td></tr>")
                                    }
                                }
                            })
                            $(".addMenu-rid").val(rid)
                        })
                        var malert= {
                            title: "Tip",
                            msg: "操作成功",
                        }
                        $(".GO").click(function (){
                            MyAlert(malert);
                        })
                        $(".fff").submit(function (){
                            $(this).ajaxSubmit()
                            return false;
                        })
                        //取消分配权限
                        $(".noMenu").click(function (){
                            $(".allRole").show();
                            $(".addMenu").hide();
                        })
                        //删除角色
                        $(".btn-danger").click(function (){
                            let rid= $(this).parents("tr").find("#rid").text()
                            $.ajax({
                                url:"deleteRole",
                                method:"post",
                                data:{rid:rid},
                                success:function (){
                                    alert("删除成功，请刷新页面")
                                },
                                error:function (){
                                    window.location.herf="404.jsp"
                                }
                            })
                        })
                    },
                    error:function (){
                        window.location.herf="404.jsp"
                    },
                })
            }
        })
        })
</script>
</body>
</html>
